# 数据模型层
import json
class MysqlDatabases:
    # 构造函数中读取json文件的内容
    def __init__(self):
        with open('users.json', 'r', encoding='utf-8') as f:
            # test = f.read()
            self.users = json.loads(f.read())

    # 判断密码是否正确逻辑
    def check_login(self, username, password):
        for user in self.users:
            if username == user['username']:
                if password == user['password']:
                    return True, '登陆成功'
                else:
                    return False, '密码错误,登录失败'

        return False, '账号错误,登录失败'


    def show_all_data(self):
        # 读取json中的学生数据
        with open('students.json', 'r', encoding='utf-8') as f:
            self.loaded_data = json.loads(f.read())

    def json_insert(self,d):
        self.loaded_data.append(d)
        with open('students.json', 'w', encoding='utf-8') as f:
            json.dump(self.loaded_data, f)

    def json_del(self,d):
        self.loaded_data = d
        with open('students.json', 'w', encoding='utf-8') as f:
            json.dump(self.loaded_data, f)

db = MysqlDatabases()

if __name__ == '__main__':
    print(db.check_login('admin','123456'))